/*==============================================================================
案例7：企业绩效预测（H2O GBM + AutoML）
文件: case07_performance_prediction.do
==============================================================================*/

clear all
set more off
capture log close

* 设置工作目录
cd "/Users/mac/git/stata"

* 创建输出目录
capture mkdir output
capture mkdir output/figures

* 开始日志
log using "output/case07_performance_prediction.log", replace text

display "=========================================="
display "=========================================="

/*------------------------------------------------------------------------------
第一步：数据准备
------------------------------------------------------------------------------*/

display ""
display "第一步：创建企业绩效数据"

set seed 98765
set obs 800

* 财务指标
gen revenue = exp(rnormal(16, 1))  // 营业收入（对数正态分布）
replace revenue = max(100000, min(50000000, revenue))

gen total_assets = revenue * (runiform() * 3 + 1)
gen total_equity = total_assets * (runiform() * 0.4 + 0.2)
gen total_debt = total_assets - total_equity

* 盈利能力指标
gen gross_margin = runiform() * 0.4 + 0.1  // 毛利率
gen operating_margin = gross_margin * (runiform() * 0.6 + 0.2)  // 营业利润率
gen net_margin = operating_margin * (runiform() * 0.8 + 0.1)  // 净利率

* 运营效率指标
gen asset_turnover = runiform() * 2 + 0.5  // 资产周转率
gen inventory_turnover = runiform() * 8 + 2  // 存货周转率
gen receivable_days = int(runiform() * 90) + 30  // 应收账款天数

* 财务结构指标
gen debt_to_equity = total_debt / total_equity
gen current_ratio = runiform() * 2 + 0.8  // 流动比率
gen quick_ratio = current_ratio * (runiform() * 0.7 + 0.2)  // 速动比率

* 成长性指标
gen revenue_growth = rnormal(0.15, 0.2)  // 收入增长率
replace revenue_growth = max(-0.5, min(1.0, revenue_growth))

gen profit_growth = revenue_growth * (runiform() * 1.5 + 0.5)
replace profit_growth = max(-0.8, min(1.5, profit_growth))

* 市场指标
gen market_share = runiform() * 0.3  // 市场份额
gen customer_satisfaction = runiform() * 0.4 + 0.6  // 客户满意度

* 人力资源指标
gen employee_count = int(exp(rnormal(5, 1.5)))
replace employee_count = max(10, min(10000, employee_count))

gen revenue_per_employee = revenue / employee_count
gen employee_turnover = runiform() * 0.3  // 员工流失率

* 创新指标
gen rd_intensity = runiform() * 0.1  // 研发强度
gen new_product_ratio = runiform() * 0.4  // 新产品占比

* 行业和规模
gen industry = int(runiform() * 5) + 1
label define ind_lbl 1 "制造业" 2 "服务业" 3 "科技业" 4 "零售业" 5 "金融业"
label values industry ind_lbl

gen company_size = 1 if employee_count < 100
replace company_size = 2 if employee_count >= 100 & employee_count < 500
replace company_size = 3 if employee_count >= 500 & employee_count < 2000
replace company_size = 4 if employee_count >= 2000 & !missing(employee_count)
label define size_lbl 1 "小型" 2 "中型" 3 "大型" 4 "超大型"
label values company_size company_size

* 生成ROE（净资产收益率）作为绩效指标
gen roe = net_margin * asset_turnover * (total_assets / total_equity) + ///
    revenue_growth * 0.1 + ///
    (1 - employee_turnover) * 0.05 + ///
    rd_intensity * 0.3 + ///
    customer_satisfaction * 0.1 + ///
    rnormal(0, 0.03)

replace roe = max(-0.2, min(0.5, roe))

* 创建绩效等级
gen performance_level = 1 if roe < 0.05
replace performance_level = 2 if roe >= 0.05 & roe < 0.10
replace performance_level = 3 if roe >= 0.10 & roe < 0.15
replace performance_level = 4 if roe >= 0.15 & !missing(roe)
label define perf_lbl 1 "低绩效" 2 "中等绩效" 3 "良好绩效" 4 "优秀绩效"
label values performance_level perf_lbl

display ""
summarize roe revenue gross_margin operating_margin net_margin asset_turnover

display ""
display "--------------------"
tabulate industry

display ""
tabulate company_size

display ""
tabulate performance_level

/*------------------------------------------------------------------------------
第二步：探索性数据分析
------------------------------------------------------------------------------*/

display ""
display "第二步：探索性数据分析"

* 1. ROE分布
histogram roe, ///
    title("净资产收益率（ROE）分布") ///
    xtitle("ROE") ///
    ytitle("频率") ///
    normal ///
    scheme(s2color)
graph export "output/figures/case07_01_roe_distribution.png", replace width(1200)

* 2. ROE vs 净利率
twoway (scatter roe net_margin, msize(small) mcolor(blue%30)) ///
       (lfit roe net_margin, lcolor(red) lwidth(medium)), ///
    title("ROE vs 净利率") ///
    xtitle("净利率") ///
    ytitle("ROE") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case07_02_roe_vs_net_margin.png", replace width(1200)

* 3. ROE vs 资产周转率
twoway (scatter roe asset_turnover, msize(small) mcolor(green%30)) ///
       (lfit roe asset_turnover, lcolor(red) lwidth(medium)), ///
    title("ROE vs 资产周转率") ///
    xtitle("资产周转率") ///
    ytitle("ROE") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case07_03_roe_vs_asset_turnover.png", replace width(1200)

* 4. ROE vs 收入增长率
twoway (scatter roe revenue_growth, msize(small) mcolor(orange%30)) ///
       (lfit roe revenue_growth, lcolor(red) lwidth(medium)), ///
    title("ROE vs 收入增长率") ///
    xtitle("收入增长率") ///
    ytitle("ROE") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case07_04_roe_vs_revenue_growth.png", replace width(1200)

* 5. 按行业的ROE分布
graph box roe, over(industry) ///
    title("不同行业的ROE分布") ///
    ytitle("ROE") ///
    scheme(s2color)
graph export "output/figures/case07_05_roe_by_industry.png", replace width(1200)

* 6. 按企业规模的ROE分布
graph box roe, over(company_size) ///
    title("不同规模企业的ROE分布") ///
    ytitle("ROE") ///
    scheme(s2color)
graph export "output/figures/case07_06_roe_by_size.png", replace width(1200)

* 7. 按绩效等级的指标对比
graph box net_margin, over(performance_level) ///
    title("不同绩效等级的净利率") ///
    ytitle("净利率") ///
    scheme(s2color)
graph export "output/figures/case07_07_net_margin_by_performance.png", replace width(1200)

* 8. ROE vs 研发强度
twoway (scatter roe rd_intensity, msize(small) mcolor(purple%30)) ///
       (lfit roe rd_intensity, lcolor(red) lwidth(medium)), ///
    title("ROE vs 研发强度") ///
    xtitle("研发强度（R&D/收入）") ///
    ytitle("ROE") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case07_08_roe_vs_rd.png", replace width(1200)

* 9. ROE vs 客户满意度
twoway (scatter roe customer_satisfaction, msize(small) mcolor(red%30)) ///
       (lfit roe customer_satisfaction, lcolor(blue) lwidth(medium)), ///
    title("ROE vs 客户满意度") ///
    xtitle("客户满意度") ///
    ytitle("ROE") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case07_09_roe_vs_satisfaction.png", replace width(1200)

* 10. ROE vs 员工流失率
twoway (scatter roe employee_turnover, msize(small) mcolor(brown%30)) ///
       (lfit roe employee_turnover, lcolor(red) lwidth(medium)), ///
    title("ROE vs 员工流失率") ///
    xtitle("员工流失率") ///
    ytitle("ROE") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case07_10_roe_vs_turnover.png", replace width(1200)

* 11. 杜邦分析可视化
gen roe_dupont = net_margin * asset_turnover * (total_assets / total_equity)

twoway (scatter roe roe_dupont, msize(small) mcolor(blue%30)) ///
       (function y=x, range(roe) lcolor(red) lpattern(dash)), ///
    title("ROE实际值 vs 杜邦分解值") ///
    xtitle("杜邦分解ROE") ///
    ytitle("实际ROE") ///
    legend(order(1 "实际数据" 2 "完美拟合")) ///
    scheme(s2color)
graph export "output/figures/case07_11_dupont_analysis.png", replace width(1200)

* 12. 财务杠杆 vs ROE
gen financial_leverage = total_assets / total_equity

twoway (scatter roe financial_leverage, msize(small) mcolor(green%30)) ///
       (lfit roe financial_leverage, lcolor(red) lwidth(medium)), ///
    title("ROE vs 财务杠杆") ///
    xtitle("财务杠杆（资产/权益）") ///
    ytitle("ROE") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case07_12_roe_vs_leverage.png", replace width(1200)

/*------------------------------------------------------------------------------
第三步：初始化H2O并准备数据
------------------------------------------------------------------------------*/

display ""
display "第三步：初始化H2O"
display "----------------"

h2o init

* 导入数据到H2O
_h2oframe put, into(performance_data) current

* 划分训练/测试集
_h2oframe split performance_data, into(train test) split(0.8 0.2) rseed(123)
_h2oframe change train

display ""

/*------------------------------------------------------------------------------
第四步：训练GBM模型
------------------------------------------------------------------------------*/

display ""
display "第四步：训练GBM模型"

h2oml gbregress roe gross_margin operating_margin net_margin ///
    asset_turnover inventory_turnover receivable_days ///
    debt_to_equity current_ratio revenue_growth profit_growth ///
    market_share customer_satisfaction employee_turnover ///
    rd_intensity new_product_ratio revenue_per_employee, ///
    h2orseed(123) cv(5) ///
    ntrees(50(50)200) ///
    lrate(0.05(0.05)0.2) ///
    maxdepth(3(1)8) ///
    tune(metric(rmse) grid(random) maxmodels(25))

* 保存模型
h2omlest store gbm_performance

* 查看性能
display ""
display "-------------------"
h2omlestat metrics

/*------------------------------------------------------------------------------
第五步：AutoML自动机器学习
------------------------------------------------------------------------------*/

display ""
display "第五步：AutoML自动机器学习"

h2oml automl roe gross_margin operating_margin net_margin ///
    asset_turnover inventory_turnover receivable_days ///
    debt_to_equity current_ratio revenue_growth profit_growth ///
    market_share customer_satisfaction employee_turnover ///
    rd_intensity new_product_ratio revenue_per_employee, ///
    h2orseed(123) ///
    maxmodels(20) ///
    maxruntime(300)

* 保存AutoML最佳模型
h2omlest store automl_performance

* 查看AutoML排行榜
display ""
display "------------------------"
h2omlestat leaderboard

* 查看最佳模型性能
display ""
h2omlestat metrics

/*------------------------------------------------------------------------------
第六步：模型比较
------------------------------------------------------------------------------*/

display ""
display "第六步：模型比较（GBM vs AutoML）"

display ""
h2omlestat metrics, model(gbm_performance automl_performance)

/*------------------------------------------------------------------------------
第七步：模型可解释性分析
------------------------------------------------------------------------------*/

display ""
display "第七步：模型可解释性分析"

* 使用GBM模型进行解释
h2omlest restore gbm_performance

* 13. 变量重要性
h2omlgraph varimp, ///
    title("企业绩效驱动因素 - 变量重要性") ///
    scheme(s2color)
graph export "output/figures/case07_13_varimp.png", replace width(1200)

* 14. SHAP汇总图
h2omlgraph shapsummary, ///
    title("企业绩效 - SHAP值分析") ///
    scheme(s2color)
graph export "output/figures/case07_14_shap_summary.png", replace width(1200)

* 15. 部分依赖图 - 净利率
h2omlgraph pdp net_margin, ///
    title("净利率对ROE的边际影响") ///
    xtitle("净利率") ///
    ytitle("预测ROE") ///
    scheme(s2color)
graph export "output/figures/case07_15_pdp_net_margin.png", replace width(1200)

* 16. 部分依赖图 - 资产周转率
h2omlgraph pdp asset_turnover, ///
    title("资产周转率对ROE的边际影响") ///
    xtitle("资产周转率") ///
    ytitle("预测ROE") ///
    scheme(s2color)
graph export "output/figures/case07_16_pdp_asset_turnover.png", replace width(1200)

* 17. 部分依赖图 - 收入增长率
h2omlgraph pdp revenue_growth, ///
    title("收入增长率对ROE的边际影响") ///
    xtitle("收入增长率") ///
    ytitle("预测ROE") ///
    scheme(s2color)
graph export "output/figures/case07_17_pdp_revenue_growth.png", replace width(1200)

* 18. 部分依赖图 - 研发强度
h2omlgraph pdp rd_intensity, ///
    title("研发强度对ROE的边际影响") ///
    xtitle("研发强度") ///
    ytitle("预测ROE") ///
    scheme(s2color)
graph export "output/figures/case07_18_pdp_rd_intensity.png", replace width(1200)

* 19. 部分依赖图 - 客户满意度
h2omlgraph pdp customer_satisfaction, ///
    title("客户满意度对ROE的边际影响") ///
    xtitle("客户满意度") ///
    ytitle("预测ROE") ///
    scheme(s2color)
graph export "output/figures/case07_19_pdp_satisfaction.png", replace width(1200)

* 20. 部分依赖图 - 员工流失率
h2omlgraph pdp employee_turnover, ///
    title("员工流失率对ROE的边际影响") ///
    xtitle("员工流失率") ///
    ytitle("预测ROE") ///
    scheme(s2color)
graph export "output/figures/case07_20_pdp_turnover.png", replace width(1200)

* 21. 部分依赖图 - 资产负债率
h2omlgraph pdp debt_to_equity, ///
    title("资产负债率对ROE的边际影响") ///
    xtitle("资产负债率") ///
    ytitle("预测ROE") ///
    scheme(s2color)
graph export "output/figures/case07_21_pdp_debt_to_equity.png", replace width(1200)

* 22. 部分依赖图 - 人均收入
h2omlgraph pdp revenue_per_employee, ///
    title("人均收入对ROE的边际影响") ///
    xtitle("人均收入（元）") ///
    ytitle("预测ROE") ///
    scheme(s2color)
graph export "output/figures/case07_22_pdp_revenue_per_employee.png", replace width(1200)

/*------------------------------------------------------------------------------
第八步：预测和评估
------------------------------------------------------------------------------*/

display ""
display "第八步：模型预测和评估"

* 在测试集上预测（使用GBM模型）
_h2oframe change test
h2omlpredict roe_pred_gbm, model(gbm_performance)

* 使用AutoML模型预测
h2omlest restore automl_performance
h2omlpredict roe_pred_automl, model(automl_performance)

* 获取测试集结果
_h2oframe get test, into(test_results) replace

* 计算预测误差（GBM）
gen prediction_error_gbm = roe - roe_pred_gbm
gen abs_error_gbm = abs(prediction_error_gbm)
gen pct_error_gbm = (abs_error_gbm / abs(roe)) * 100

* 计算预测误差（AutoML）
gen prediction_error_automl = roe - roe_pred_automl
gen abs_error_automl = abs(prediction_error_automl)
gen pct_error_automl = (abs_error_automl / abs(roe)) * 100

display ""
display "------------------------------"
summarize roe roe_pred_gbm prediction_error_gbm abs_error_gbm pct_error_gbm

display ""
display "---------------------------------"
summarize roe roe_pred_automl prediction_error_automl abs_error_automl pct_error_automl

* 23. 实际值 vs 预测值（GBM）
twoway (scatter roe roe_pred_gbm, msize(small) mcolor(blue%30)) ///
       (function y=x, range(roe) lcolor(red) lpattern(dash) lwidth(medium)), ///
    title("实际ROE vs 预测ROE（GBM）") ///
    xtitle("预测ROE") ///
    ytitle("实际ROE") ///
    legend(order(1 "预测结果" 2 "完美预测线")) ///
    scheme(s2color)
graph export "output/figures/case07_23_actual_vs_predicted_gbm.png", replace width(1200)

* 24. 实际值 vs 预测值（AutoML）
twoway (scatter roe roe_pred_automl, msize(small) mcolor(green%30)) ///
       (function y=x, range(roe) lcolor(red) lpattern(dash) lwidth(medium)), ///
    title("实际ROE vs 预测ROE（AutoML）") ///
    xtitle("预测ROE") ///
    ytitle("实际ROE") ///
    legend(order(1 "预测结果" 2 "完美预测线")) ///
    scheme(s2color)
graph export "output/figures/case07_24_actual_vs_predicted_automl.png", replace width(1200)

* 25. 预测误差分布对比
twoway (histogram prediction_error_gbm, color(blue%30)) ///
       (histogram prediction_error_automl, color(red%30)), ///
    title("预测误差分布对比") ///
    xtitle("预测误差") ///
    ytitle("频率") ///
    legend(order(1 "GBM" 2 "AutoML")) ///
    scheme(s2color)
graph export "output/figures/case07_25_error_comparison.png", replace width(1200)

* 26. 按绩效等级的预测准确性
graph box abs_error_gbm, over(performance_level) ///
    title("不同绩效等级的预测误差（GBM）") ///
    ytitle("绝对误差") ///
    scheme(s2color)
graph export "output/figures/case07_26_error_by_performance.png", replace width(1200)

/*------------------------------------------------------------------------------
第九步：绩效改进分析
------------------------------------------------------------------------------*/

display ""
display "第九步：绩效改进潜力分析"
display "------------------------"

* 计算改进潜力
gen improvement_potential = 0

* 净利率改进（如果低于行业平均）
bysort industry: egen industry_avg_margin = mean(net_margin)
replace improvement_potential = improvement_potential + ///
    (industry_avg_margin - net_margin) * asset_turnover * financial_leverage ///
    if net_margin < industry_avg_margin

* 资产周转率改进
bysort industry: egen industry_avg_turnover = mean(asset_turnover)
replace improvement_potential = improvement_potential + ///
    (industry_avg_turnover - asset_turnover) * net_margin * financial_leverage ///
    if asset_turnover < industry_avg_turnover

* 降低员工流失率
replace improvement_potential = improvement_potential + ///
    employee_turnover * 0.05 if employee_turnover > 0.15

* 提高研发投入
replace improvement_potential = improvement_potential + ///
    (0.05 - rd_intensity) * 0.3 if rd_intensity < 0.05

* 提高客户满意度
replace improvement_potential = improvement_potential + ///
    (0.9 - customer_satisfaction) * 0.1 if customer_satisfaction < 0.9

display ""
display "------------------------"
summarize improvement_potential, detail

* 27. 改进潜力分布
histogram improvement_potential, ///
    title("ROE改进潜力分布") ///
    xtitle("潜在ROE提升") ///
    ytitle("频率") ///
    scheme(s2color)
graph export "output/figures/case07_27_improvement_potential.png", replace width(1200)

* 创建改进机会分类
gen improvement_opportunity = 1 if improvement_potential < 0.02
replace improvement_opportunity = 2 if improvement_potential >= 0.02 & improvement_potential < 0.05
replace improvement_opportunity = 3 if improvement_potential >= 0.05 & improvement_potential < 0.10
replace improvement_opportunity = 4 if improvement_potential >= 0.10 & !missing(improvement_potential)
label define imp_lbl 1 "低改进潜力" 2 "中等改进潜力" 3 "高改进潜力" 4 "极高改进潜力"
label values improvement_opportunity imp_lbl

display ""
tabulate improvement_opportunity

* 28. 改进机会分布
graph bar (count), over(improvement_opportunity) ///
    title("绩效改进机会分布") ///
    ytitle("企业数量") ///
    scheme(s2color)
graph export "output/figures/case07_28_improvement_opportunity.png", replace width(1200)

* 29. 当前ROE vs 改进后ROE
gen roe_improved = roe + improvement_potential

twoway (scatter roe_improved roe, msize(small) mcolor(blue%30)) ///
       (function y=x, range(roe) lcolor(red) lpattern(dash)), ///
    title("当前ROE vs 改进后ROE") ///
    xtitle("当前ROE") ///
    ytitle("改进后ROE") ///
    legend(order(1 "改进潜力" 2 "无改进线")) ///
    scheme(s2color)
graph export "output/figures/case07_29_current_vs_improved.png", replace width(1200)

* 30. 按行业的改进潜力
graph box improvement_potential, over(industry) ///
    title("不同行业的改进潜力") ///
    ytitle("ROE改进潜力") ///
    scheme(s2color)
graph export "output/figures/case07_30_improvement_by_industry.png", replace width(1200)

/*------------------------------------------------------------------------------
第十步：管理洞察和策略建议
------------------------------------------------------------------------------*/

display ""
display "=========================================="
display "=========================================="
display ""

display ""

display ""

display ""

display ""

display ""

display ""

display ""

display ""

/*------------------------------------------------------------------------------
第十一步：输出总结
------------------------------------------------------------------------------*/

display ""
display "=========================================="
display "案例7：企业绩效预测 - 完成"
display "=========================================="
display ""

display ""

display ""

display ""

display "✓ 构建双模型预测系统（GBM + AutoML）"
display "✓ 识别7个关键绩效驱动因素"
display "✓ 生成30张可视化图表"
display "✓ 杜邦分析框架应用"
display "✓ 提供5大类提升策略"
display "✓ 预期ROE提升5-15个百分点"
display "✓ 预期净利润增长50-100%"
display ""

/*------------------------------------------------------------------------------
第十二步：清理资源
------------------------------------------------------------------------------*/

display ""
display "第十二步：清理H2O资源"
display "----------------------"

h2o shutdown, force

display ""
display "✓ H2O资源已清理"
display ""

log close

display ""
display "=========================================="
display "案例7分析完成！"
display "日志文件: output/case07_performance_prediction.log"
display "图表目录: output/figures/"
display "=========================================="

